const { ipcMain } = require('electron');
const winManager = require('../manager/winManager');
const fs = require('fs');
const path = require('path');

let App = null;
let Share = {};
function select(model) {
    return Share[model].state;
}

function listenProxyShare() {
    ipcMain.handle('user-proxy-share', (event, arg) => {
        const { model, reducer, arguments } = arg;
        if(Share[model] && Share[model].effects) {
            if (typeof Share[model].effects[reducer] === 'function') {
                Share[model]
                    .effects[reducer](
                        { state: Share[model].state, payload: arguments },
                        {
                            select,
                            dispatch: (option) => {
                                const { type, payload } = option;
                                const modelAndReducer = type.split('/');
                                let targetModel = model;
                                let reducer = '';
                                if (modelAndReducer.length === 2) {
                                    targetModel = modelAndReducer[0];
                                    reducer = modelAndReducer[1];
                                } else {
                                    reducer = modelAndReducer[0];
                                }
                                if (Share[targetModel] && Share[targetModel].reducers) {
                                    if (typeof Share[targetModel].reducers[reducer] === 'function') {
                                        const newState = Share[targetModel].reducers[reducer](Share[targetModel].state, { payload });
                                        Share[targetModel].state = newState;
                                        sendToUser();
                                    }
                                }
                            }
                        });

            }
        } else if (Share[model] && Share[model].reducers) {
            if (typeof Share[model].reducers[reducer] === 'function') {
                const newState = Share[model].reducers[reducer](Share[model].state, { payload: arguments });
                Share[model].state = newState;
                sendToUser();
            }
        }
    })
}

function sendToUser() {
    let state = {};
    for (let key in Share) {
        state[key] = Share[key].state;
    }
    const wins = winManager.getWinSet();
    for(let key in wins) {
        wins[key].webContents.send('sys-get-share', state);
    }
}

function listenGetShare() {
    ipcMain.handle('user-get-share', (event, arg) => {
        let state = {};
        console.log(Share);
        for (let key in Share) {
            state[key] = Share[key].state;
        }
        console.log(state);
       return state;
    });
}


function initShare() {
    const dir = path.resolve(__dirname, '../models');
    if(fs.existsSync(dir)){
        let files = fs.readdirSync(dir);
            console.log(files);
            files.map(item => {
            const name = item.substring(0, item.indexOf('.'));
            Share[name] = require(`../models/${item}`);

        });
    }
}

exports.use = function(app) {
    App = app;
    initShare();
    listenGetShare()
    listenProxyShare();
}
